package com.socialnetspace.controller;

import java.util.logging.Logger;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

@Controller
public class BasicController {
	
	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	
	@RequestMapping(value = {"/register"}, method=RequestMethod.GET)
	public String getRegistrationPage() {
		
		logger.fine("Fetching registration page ...");
		
		return "register";
	}
	
	
	@RequestMapping(value = {"/register"}, method=RequestMethod.POST)
	public String getLoginPage(WebRequest request) {
		
		logger.fine("Registering user ...");
		
		String firstName = request.getParameter("firstName");
		logger.finer(firstName);
		
		String lastName = request.getParameter("lastName");
		logger.finer(lastName);
		
		return "redirect:/login";
	}
	
	
	@RequestMapping(value = "/login", method=RequestMethod.GET)
	public String getLogin(Model model, @RequestParam(
			value="message", required=false) String message) {
		
		logger.fine("Fetching login page ...");
		
		Object principal = SecurityContextHolder.getContext().
				getAuthentication().getPrincipal();
		
		if (principal instanceof UserDetails) {
			  String username = ((UserDetails)principal).getUsername();
			  logger.fine("User "+username+" is authenticated. Redirecting ...");
			  return "redirect:/home";
		}
		model.addAttribute("message", message);
		
		return "login";
	}
	
	
	@RequestMapping(value = {"/home"}, method=RequestMethod.GET)
	public String getHomePage() {
		
		logger.fine("Fetching home ...");
		
		return "home";
	}
	
	
	@RequestMapping(value = {"/profile"}, method=RequestMethod.GET)
	public String getProfilePage() {
		
		logger.fine("Fetching profile ...");
		
		return "profile";
	}
	
	
	@RequestMapping(value = {"/settings"}, method=RequestMethod.GET)
	public String getSettingsPage() {
		
		logger.fine("Fetching settings ...");
		
		return "settings";
	}
	
	
	@RequestMapping(value = {"/twitter"}, method=RequestMethod.GET)
	public String getTwitterPage() {
		
		logger.fine("Fetching twitter ...");
		
		return "twitter";
	}
	
	
	@RequestMapping(value = {"/facebook"}, method=RequestMethod.GET)
	public String getFacebookPage() {
		
		logger.fine("Fetching facebook ...");
		
		return "facebook";
	}
	
	
	@RequestMapping(value = {"/dashboard"}, method=RequestMethod.GET)
	public String getDashboard(Model model) {
		
		logger.fine("Fetching dashboard ...");
		
		return "dashboard";
	}
	
	
	@RequestMapping(value = {"/logout*", "/logout/**"}, method=RequestMethod.GET)
 	public String logoutSuccess() {
		
		String message = "Logout Success!";
		
		return "redirect:/login?message="+message;
	}
	
	
	@RequestMapping(value = {"/error"}, method=RequestMethod.GET)
	public String getErrorPage(Model model) {
		
		logger.fine("Fetching error page ...");
		
		return "error";
	}
	
	
	@RequestMapping(value = {"/unauthorized"}, method=RequestMethod.GET)
	public String getAccessDeniedPage(Model model) {
		
		logger.fine("Fetching access denied page ...");
		
		return "unauthorized";
	}
	
}